package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.common.Result;
import com.amsp.oa.oademo.entity.DocumentDistribution;
import com.amsp.oa.oademo.service.DocumentDistributionService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "文件发放管理")
@RestController
@RequestMapping("/api/document-distribution")
public class DocumentDistributionController {

    private static final Logger log = LoggerFactory.getLogger(DocumentDistributionController.class);

    @Autowired
    private DocumentDistributionService documentDistributionService;

    @ApiOperation("分页查询文件发放记录")
    @GetMapping("/page")
    public Result<IPage<DocumentDistribution>> getPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String fileName,
            @RequestParam(required = false) String fileNumber) {
        return Result.ok(documentDistributionService.getPage(current, size, fileName, fileNumber));
    }

    @ApiOperation("获取文件发放记录详情")
    @GetMapping("/{id}")
    public Result<DocumentDistribution> getDetail(@PathVariable Long id) {
        return Result.ok(documentDistributionService.getDetailById(id));
    }

    @ApiOperation("保存或更新文件发放记录")
    @PostMapping("/save")
    public Result<Boolean> saveOrUpdate(@RequestBody DocumentDistribution distribution) {
        log.info("接收到的保存数据: {}", distribution);
        boolean result = documentDistributionService.saveOrUpdateDistribution(distribution);
        log.info("保存结果: {}", result);
        return Result.ok(result);
    }

    @ApiOperation("删除文件发放记录")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.ok(documentDistributionService.deleteById(id));
    }
} 